﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Component.Base;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;

namespace OP.manage.ajax
{
    public partial class api : OpBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            switch (Request["action"])
            {
                case "Login":
                    Login();
                    break;
                case "getprovince":
                    GetProvinve();
                    break;
                case "getcity":
                    GetCity();
                    break;
                case "getarea":
                    GetArea();
                    break;
                case "checkCode":
                    IsPassCheckMenuCode();
                    break;

             
                default:
                    break;
            }
        }

        public void Login()
        {
            string szReturn = AjaxResult.Error("未知错误").ToString();
            string strUserName = RequestUtility.GetString("UserName");
            string strPassWord = RequestUtility.GetString("Password");
            string validateCode = RequestUtility.GetString("ValidateCode");
            if (Session["CheckCode"] != null)
            {
                if (Session["CheckCode"].ToString().ToUpper() != validateCode.ToUpper())
                {
                    szReturn = AjaxResult.Error("验证码不正确请重新输入").ToString();
                }
            }
            OpUserInforInfo info = OpUserInforOperate.GetByLoginName(strUserName);
            if (info != null)
            {
                var enPwd = SecurityUtility.EncryptAsc(strPassWord);
                if (info.PassWord == enPwd)
                {
                    OpUserInforManager.Login(info.OpUserID);
                    string loginUrl = string.Format(SiteUtility.GetBaseURL() + "/default.aspx");
                    szReturn = AjaxResult.Success(loginUrl,"登录成功").ToString();
                }
                else
                {
                    szReturn = AjaxResult.Error("用户名或密码不正确请重新输入！").ToString();
                }
            }
            else
            {
                szReturn = AjaxResult.Error("用户名或密码不正确请重新输入！").ToString();
            }

            Response.Write(szReturn);
        }
        
        ///<summary>
        ///是否通过验证
        ///</summary>
        public void IsPassCheckMenuCode()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int code = RequestUtility.GetInt("code", -1);
            int id = RequestUtility.GetInt("menuId", -1);
            if (id > 0)
            {
                //修改时先验证是否跟当前代码相同，如果不同则验证是否重复
                AuthorityMenuInfo menuObj = AuthorityMenuOperate.GetByID(id);
                if (menuObj != null)
                {
                    if (menuObj.MenuCode == code)
                    {
                        szReturn = AjaxResult.Success("true").ToString();
                    }
                    else
                    {
                        bool isTrue = AuthorityMenuOperate.CheckMenuCodeIsExist(code);
                        if (!isTrue)
                        {
                            szReturn = AjaxResult.Success("true").ToString();
                        }
                    }
                }
            }
            else
            {
                bool isTrue = AuthorityMenuOperate.CheckMenuCodeIsExist(code);
                if (!isTrue)
                {
                    szReturn = AjaxResult.Success("true").ToString();
                }
            }

            Response.Write(szReturn);
        }
        private void GetProvinve()
        {
            string szReturn = AjaxResult.Error("").ToString();
            var dt = BaseLocationOperate.GetProvinceList();
            var pList = (from DataRow row in dt.Rows
                         select new
                         {
                             ProvinceId = row["ProvinceId"],
                             ProvinceName = row["ProvinceName"]
                         });
            if (dt.Rows.Count>0)
            {
                szReturn = AjaxResult.Success(pList).ToString();
            }
            Response.Write(szReturn);
        }
        private void GetCity()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int provinceId = RequestUtility.GetInt("provinceId", 0);
            var dt = BaseLocationOperate.GetCityList(provinceId);
            var pList = (from DataRow row in dt.Rows
                         select new
                         {
                             CityId = row["CityId"],
                             CityName = row["CityName"]
                         });
            if (dt.Rows.Count > 0)
            {
                szReturn = AjaxResult.Success(pList).ToString();
            }
            Response.Write(szReturn);
        }

        private void GetArea()
        {
            string szReturn = AjaxResult.Error("").ToString();
            int cityId = RequestUtility.GetInt("cityId", 0);
            var dt = BaseLocationOperate.GetAreaList(cityId);
            var pList = (from DataRow row in dt.Rows
                         select new
                         {
                             AreaId = row["AreaId"],
                             AreaName = row["AreaName"]
                         });
            if (dt.Rows.Count > 0)
            {
                szReturn = AjaxResult.Success(pList).ToString();
            }
            Response.Write(szReturn);
        }



    }
}